<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Advertise extends AdminBackEndController {
    public function __construct() {
        parent::__construct();
        $this->load->model('repo/advertise_model', 'advertise_model');
    }

    public function index() {
        $this->load->model('repo/user_model', 'user_model');
        $aAdvertise = $this->advertise_model->getAllAdvertise(false);
        $aAdvertiseRegister = $this->advertise_model->getRegisterAdvertise();
        $aUser = $this->user_model->getAllUser(array('a.id', 'full_name'));
        $aReturn = array('pageTitle'=>'Quảng cáo', 'aAdvertise'=>$aAdvertise,'aAdvertiseRegister'=>$aAdvertiseRegister,'aUser'=>$aUser);
        $this->renderView('backend/admin/advertise/index', $aReturn);
    }

    public function insert(){
        $this->load->helper('file');
        $this->load->model('repo/user_model', 'user_model');
        $aUser = $this->user_model->getAllUser(array('a.id', 'full_name', 'email'));
        $advertiseInfo = new Advertise_Entity();
        $advertiseInfo->setOrder(1);
        $advertiseInfo->setPublishFlg(1);
        $aReturn = array(
            'pageTitle'     => 'Thêm mới | Quảng cáo',
            'aUser'         => $aUser,
            'action'        => 'Thêm mới',
            'actionLink'    => admin_page_url(ADVERTISE_SLUG.'/them-moi/do'),
            'advertiseInfo' => $advertiseInfo
        );
        $this->renderView('backend/admin/advertise/single', $aReturn);
    }

    public function get($id){
        $advertiseInfo = $this->advertise_model->getById($id);
        if(count($advertiseInfo)==0){
            show_404();
        }
        $this->load->helper('file');
        $this->load->model('repo/user_model', 'user_model');
        $aUser = $this->user_model->getAllUser(array('a.id', 'full_name', 'email'));
        $aReturn = array(
            'pageTitle'     => 'Cập nhật | Quảng cáo',
            'aUser'         => $aUser,
            'action'        => 'Cập nhật',
            'advertiseInfo' => $advertiseInfo,
            'actionLink'    => admin_page_url(ADVERTISE_SLUG.'/cap-nhat/do')
        );
        $this->renderView('backend/admin/advertise/single', $aReturn);
    }

    public function doInsert(){
        $aResult    = array();
        $advertise  = $this->input->post('advertise', true);
        $tAdvertise = $this->input->post('advertise');

        //decode userid
        if(isset($advertise['user_id']) && $advertise['user_id']!=''){
            $advertise['user_id'] = $this->encrypt->decode($tAdvertise['user_id']);
            if($advertise['user_id'] == false){
                $aResult = array(
                    'code' => 0,
                    'title'=> $this->lang->line('system_error')
                );
                $this->renderJSON($aResult);
                return;
            }
        } else {
            $this->form_validation->set_rules('advertise[name]',  'lang:advertise_name',  'trim|required|max_length[50]');
            $this->form_validation->set_rules('advertise[email]', 'lang:advertise_email', 'trim|required|valid_email|max_length[150]');

            $advertise['opt_info'] = serialize(array(
                'name'  => $advertise['name'],
                'email' => $advertise['email']
            ));
            $advertise['user_id'] = NULL;
        }
        unset($advertise['name']);
        unset($advertise['email']);
        $this->form_validation->set_rules('advertise[position]',   'lang:advertise_position',   'trim|required|integer|less_than[15]|xss_clean');
        $this->form_validation->set_rules('advertise[order]',      'lang:advertise_order',      'trim|required|integer|less_than[15]|xss_clean');
        $this->form_validation->set_rules('advertise[anchor]',     'lang:advertise_anchor',     'trim|required|callback_anchor_check');
        $this->form_validation->set_rules('advertise[start_date]', 'lang:advertise_start_date', 'trim|required|callback_start_date_check');
        $this->form_validation->set_rules('advertise[end_date]',   'lang:advertise_end_date',   'trim|required|callback_end_date_check['.$advertise['start_date'].']');
        $this->form_validation->set_rules('advertise[content]',    'lang:advertise_content',    'trim|required');
        $this->form_validation->set_error_delimiters('<li>', '</li>');

        if ($this->form_validation->run() == false) {
            $aResult = array(
                'code' => 0,
                'title'=> $this->lang->line('insert_fail'),
                'text' => '<ul>' . validation_errors() . '</ul>'
                );
        } else {
            $advertise['content'] = $tAdvertise['content'];
            $advertise['created_at'] = get_current_date();
            $result = $this->advertise_model->insert($advertise);
            if($result!=false && $result > 0){
                $aResult = array(
                    'code' => 1,
                    'text'=>$this->lang->line('insert_success'),
                    'id' => $result
                );
            } else {
                $aResult = array(
                    'code' => 0,
                    'title' => $this->lang->line('system_error') . 'aaaa'
                );
            }
        }
        $this->renderJSON($aResult);
    }

    public function doUpdate(){
        $aResult      = array();
        $advertise    = $this->input->post('advertise', true);
        $tAdvertise   = $this->input->post('advertise');
        $advertise_id = isset($advertise['id']) ? $this->encrypt->decode($tAdvertise['id']) : false;

        if($advertise_id==false){
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('system_error')
            );
        } else {
            if($this->input->post('case')!=false && $this->input->post('case')==UPDATE_FULL){
                $aResult = $this->fullUpdate($advertise_id, $advertise, $tAdvertise);
            } else {
                $aResult = $this->flashUpdate($advertise_id, $advertise);
            }
        }
        $this->renderJSON($aResult);
    }

    private function fullUpdate($advertise_id, $advertise, $tAdvertise){
        $this->form_validation->set_rules('advertise[position]',   'lang:advertise_position',   'trim|required|integer|less_than[15]|xss_clean');
        $this->form_validation->set_rules('advertise[order]',      'lang:advertise_order',      'trim|required|integer|less_than[15]|xss_clean');
        $this->form_validation->set_rules('advertise[anchor]',     'lang:advertise_anchor',     'trim|required|callback_anchor_check');
        $this->form_validation->set_rules('advertise[start_date]', 'lang:advertise_start_date', 'trim|required|callback_start_date_check');
        $this->form_validation->set_rules('advertise[end_date]',   'lang:advertise_end_date',   'trim|required|callback_end_date_check['.$advertise['start_date'].']');

        if(isset($advertise['content'])){
            $this->form_validation->set_rules('advertise[content]',     'lang:advertise_content',    'trim|required');
        }

        $this->form_validation->set_error_delimiters('<li>', '</li>');

        if ($this->form_validation->run() == false) {
            $aResult = array(
                'code' => 0,
                'title'=> $this->lang->line('update_fail'),
                'text' => '<ul>'.validation_errors().'</ul>'
            );
        } else {
            //check if full update need change user id
            if(isset($advertise['user_id'])){
                $advertise_uid = isset($advertise['user_id']) && $advertise['user_id']!='' ? $this->encrypt->decode($tAdvertise['user_id']) : false;
                if(!$advertise_uid){
                    $advertise['opt_info'] = serialize(array(
                        'name'  => $advertise['name'],
                        'email' => $advertise['email']
                    ));
                    $advertise['user_id'] = NULL;
                } else {
                    $advertise['user_id'] = $advertise_uid;
                    $advertise['opt_info'] = NULL;
                }
                unset($advertise['name']);
                unset($advertise['email']);
            }

            $advertise['id'] = $advertise_id;
            $date = get_current_date();

            //set flag
            if(isset($advertise['delete_flg']) && $advertise['delete_flg']==1){
                if($advertise['delete_flg']==1){
                    $advertise['deleted_at'] = $date;
                } else {
                    $advertise['deleted_at'] = NULL;
                    $advertise['updated_at'] = $date;
                }
                unset($advertise['delete_flg']);
            } else {
                $advertise['deleted_at'] = NULL;
                $advertise['updated_at'] = $date;
            }
            if(isset($advertise['publish_flg']) && $advertise['publish_flg']==1){
                $advertise['publish_flg'] = 1;
            } else {
                $advertise['publish_flg'] = 0;
            }

            //set content
            if(isset($advertise['content'])){
                $advertise['content'] = $tAdvertise['content'];
            }

            //set image

            $result = $this->advertise_model->update($advertise_id, $advertise);
            if($result){
                $aResult = array(
                    'code' => 1,
                    'text'=>$this->lang->line('update_success')
                );
            } else {
                $aResult = array(
                    'code' => 0,
                    'title' => $this->lang->line('system_error')
                );
            }
        }
        return $aResult;
    }

    private function flashUpdate($advertise_id, $advertise){
        $date = get_current_date();
        $tAd = array();

        if(isset($advertise['delete_flg'])){
            if($advertise['delete_flg']==1){
                $tAd['deleted_at'] = $date;
            } else {
                $tAd['deleted_at'] = NULL;
                $tAd['updated_at'] = $date;
            }
        }

        if(isset($advertise['publish_flg'])){
            if($advertise['publish_flg']==1){
                $tAd['publish_flg'] = 1;
            } else {
                $tAd['publish_flg'] = 0;
            }
            $tAd['updated_at'] = $date;
        }

        if(count($tAd)>0){
            $result = $this->advertise_model->update($advertise_id, $tAd);
            if($result){
                $aResult = array(
                    'code' => 1,
                    'text'=>$this->lang->line('update_success')
                    );
            } else {
                $aResult = array(
                    'code' => 0,
                    'title' => $this->lang->line('system_error')
                    );
            }
        } else {
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('system_error')
                );
        }

        return $aResult;
    }

    public function doTempRegisterUpdate(){
        $date = get_current_date();
        $advertise = $this->input->post('advertise', true);
        $tAdvertise = $this->input->post('advertise');
        $tAd = array();

        if(isset($advertise['delete_flg'])){
            if($advertise['delete_flg']==1){
                $tAd['deleted_at'] = $date;
            } else {
                $tAd['deleted_at'] = NULL;
            }
        } else {
            $tAd['deleted_at'] = NULL;
        }

        $advertise_id = $this->encrypt->decode($tAdvertise['id']);
        if($advertise_id==false){
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('system_error')
                );
        } else {
            $result = $this->advertise_model->updateTempRegister($advertise_id, $tAd);
            if($result){
                $aResult = array(
                    'code' => 1,
                    'text'=>$this->lang->line('update_success')
                    );
            } else {
                $aResult = array(
                    'code' => 0,
                    'title' => $this->lang->line('system_error')
                    );
            }
        }

        $this->renderJSON($aResult);
    }
}
?>